import { get, post, put } from '@/utils/request'

// 知识库类型枚举
export enum KnowledgeType {
  DOCUMENT = 0,
  WEB_PAGE = 1,
  IMAGE = 2
}

// 知识库类型描述映射
export const KnowledgeTypeDesc = {
  [KnowledgeType.DOCUMENT]: '文档',
  [KnowledgeType.WEB_PAGE]: '网页',
  [KnowledgeType.IMAGE]: '图片'
}

// 创建知识库请求参数
export interface CreateKnowledgeRequest {
  name: string
  knowledgeType: KnowledgeType
  desc: string
}

// 更新知识库请求参数
export interface UpdateKnowledgeRequest {
  knowledgeId: number
  name?: string
  knowledgeType?: KnowledgeType
  desc?: string
}

// 知识库列表查询参数
export interface KnowledgeListRequest {
  keyword?: string
  page?: {
    page: number
    limit: number
    order?: 'ASC' | 'DESC'
    field?: string
  }
}

// 知识库详情
export interface KnowledgeBase {
  id: number
  name: string
  knowledgeType: KnowledgeType
  indexName: string
  indexId: string
  desc: string
  createTime: number
  updateTime: number
  documentCount: number
}

// 分页响应接口
export interface PageResponse<T> {
  pageNo: number
  pageSize: number
  totalCount: number
  totalPage: number
  data: T[]
}

// STS临时凭证响应
export interface StsResponse {
  accessKeyId: string
  accessKeySecret: string
  securityToken: string
  bucketName: string
  region: string
  filePath: string
}

// 上传文件到百炼知识库请求参数
export interface UploadToBailianRequest {
  id: number
  fileNames: Array<{
    originalName: string
    realName: string
  }>
}

// 删除知识库请求参数
export interface DeleteKnowledgeRequest {
  knowledgeId: number
}

// 创建知识库
export const createKnowledge = (params: CreateKnowledgeRequest) => {
  return post<string>('/knowledge-api/knowledge/create', params)
}

// 获取用户所有知识库
export const listKnowledge = (params: KnowledgeListRequest = {}) => {
  return post<PageResponse<KnowledgeBase>>('/knowledge-api/knowledge/listKnowledge', params)
}

// 删除知识库
export const deleteKnowledge = (params: DeleteKnowledgeRequest) => {
  return post<boolean>('/knowledge-api/knowledge/delete', params)
}

// 更新知识库
export const updateKnowledge = (params: UpdateKnowledgeRequest) => {
  return post<boolean>('/knowledge-api/knowledge/update', params)
}

// 获取用户临时访问凭证
export const getKnowledgeSts = () => {
  return post<StsResponse>('/knowledge-api/knowledge/file/sts')
}

// 上传文件到百炼知识库并将文件相关信息保存到数据库中
export const uploadToBailian = (params: UploadToBailianRequest) => {
  return post<string>('/knowledge-api/knowledge/file/uploadToBailian', params)
} 